'use strict'
var UserModel = require('../../model/user');
var Data = require('../../util/data')

class User {
    async getAll(req, res, next) {
        try {
            const users = await UserModel.find({});
            res.send(users);
        } catch (err) {
            res.send(Data.throwError('获取用户数据失败'))
        }
    }
    async get(req, res, next) {
        try {
            const id = req.params.id
            const users = await UserModel.findOne({ '_id': id });
            res.send(users);
        } catch (err) {
            res.send(Data.throwError('获取用户数据失败'))
        }
    }
    async post(req, res, next) {
        try {
            const item = req.body
            const users = await UserModel.insertMany(item);
            res.send(users[0]);
        } catch (err) {
            res.send(Data.throwError('创建用户数据失败'))
        }
    }
    async delete(req, res, next) {
        try {
            const id = req.params.id
            const users = await UserModel.deleteOne({ _id: id });
            res.send(users);
        } catch (err) {
            res.send(Data.throwError('删除用户数据失败'))
        }
    }
    async put(req, res, next) {
        try {
            const id = req.params.id
            const data = req.body
            const users = await UserModel.update({ _id: id }, data);
            res.send(users);
        } catch (err) {
            res.send(Data.throwError('修改用户数据失败'))
        }
    }
}

module.exports = new User()